﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using QuanLyShopThoiTrang_BUS_WS.QuanLyShopThoiTrang_DAO_WS;
using System.Data;

namespace QuanLyShopThoiTrang_BUS_WS
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class QLSTT_BUS : System.Web.Services.WebService
    {
        QLSTT_DAOSoapClient ws = new QLSTT_DAOSoapClient();
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public bool SanPham_Them(SanPhamDTO sp)
        {
            return ws.SanPham_Them(sp);
            //List<SanPham> returnList = new List<SanPham>();
            //foreach (SanPham sp in listSP)
            //{
            //    if (ws.SanPham_KiemTraMaSP(sp))
            //        returnList.Add(sp);
            //    else
            //    {
            //        if (ws.SanPham_Them(sp))
            //        {
            //        }
            //        else
            //            returnList.Add(sp);
            //    }
            //}
            //return returnList;
        }

        [WebMethod]
        public bool SanPham_Xoa(SanPham sp)
        {
            return ws.SanPham_Xoa(sp);
        }

        [WebMethod]
        public bool SanPham_CapNhat(SanPham sp)
        {
            return ws.SanPham_CapNhat(sp);
        }

        [WebMethod]
        public List<SanPham> SanPham_TraCuu(SanPham sp)
        {
            if(sp.MaSanPham == -1 || sp.MaSanPham == -2)
                return ws.SanPham_TraCuu(sp);
            else
            {
                List<SanPham> listSP = ws.SanPham_TraCuu(sp);
                if(sp.MaLoaiSanPham != 0)
                {
                    listSP.RemoveAll(s => s.MaLoaiSanPham != sp.MaLoaiSanPham);
                }
                if (sp.MaNhaCungCap != 0)
                {
                    listSP.RemoveAll(s => s.MaNhaCungCap != sp.MaNhaCungCap);
                }
                if (sp.SanPham_GiaKhiBan != 0)
                {
                    listSP.RemoveAll(s => s.SanPham_GiaKhiBan != sp.SanPham_GiaKhiBan);
                }
                if (sp.SanPham_GiaKhiMua != 0)
                {
                    listSP.RemoveAll(s => s.SanPham_GiaKhiMua != sp.SanPham_GiaKhiMua);
                }
                if (sp.SanPham_GiaUuDai != null)
                {
                    if(sp.SanPham_GiaUuDai != 0)
                        listSP.RemoveAll(s => (s.SanPham_GiaUuDai != null ? s.SanPham_GiaUuDai : 0) != sp.SanPham_GiaUuDai);
                }
                if (sp.SanPham_HinhAnh != "")
                {
                    listSP.RemoveAll(s => s.SanPham_HinhAnh != sp.SanPham_HinhAnh);
                }
                if (sp.SanPham_KichCo != "")
                {
                    listSP.RemoveAll(s => s.SanPham_KichCo != sp.SanPham_KichCo);
                }
                if (sp.SanPham_NhanHieu != "")
                {
                    listSP.RemoveAll(s => s.SanPham_NhanHieu != sp.SanPham_NhanHieu);
                }
                if (sp.SanPham_SoLuong != -1)
                {
                    listSP.RemoveAll(s => s.SanPham_SoLuong != sp.SanPham_SoLuong);
                }
                if (sp.SanPham_Ten != "")
                {
                    listSP.RemoveAll(s => !s.SanPham_Ten.Contains(sp.SanPham_Ten));
                }
                if (sp.SanPham_XuatXu != "")
                {
                    listSP.RemoveAll(s => s.SanPham_XuatXu != sp.SanPham_XuatXu);
                }
                return listSP;
            }
        }

        [WebMethod]
        public SanPham SanPham_LaySP(SanPham sp)
        {
            return ws.SanPham_LaySP(sp);
        }

        [WebMethod]
        public SanPham SanPham_LaySPTheoMaSP(SanPham sp)
        {
            return ws.SanPham_LaySPTheoMaSP(sp);
        }

        [WebMethod]
        public LoaiSanPham SanPham_LayLoaiSP(SanPham sp)
        {
            return ws.SanPham_LayLoaiSP(sp);
        }

        [WebMethod]
        public NhaCungCap SanPham_LayNCC(SanPham sp)
        {
            return ws.SanPham_LayNCC(sp);
        }

        [WebMethod]
        public bool LoaiSanPham_Them(LoaiSanPham sp)
        {
            return ws.LoaiSanPham_Them(sp);
        }

        [WebMethod]
        public bool LoaiSanPham_Xoa(LoaiSanPham sp)
        {
            return ws.LoaiSanPham_Xoa(sp);
        }

        [WebMethod]
        public bool LoaiSanPham_CapNhat(LoaiSanPham sp)
        {
            return ws.LoaiSanPham_CapNhat(sp);
        }

        [WebMethod]
        public List<LoaiSanPham> LoaiSanPham_LayDanhSach()
        {
            return ws.LoaiSanPham_LayDanhSach();
        }

        /*[WebMethod]
        public int LoaiSanPham_LayMaLoaiSP()
        {
            return ws.LoaiSanPham_LayMaLoaiSP();
        }*/

        [WebMethod]
        public bool NhaCungCap_Them(NhaCungCap ncc)
        {
            return ws.NhaCungCap_Them(ncc);
        }

        [WebMethod]
        public bool NhaCungCap_Xoa(NhaCungCap ncc)
        {
            return ws.NhaCungCap_Xoa(ncc);
        }

        [WebMethod]
        public bool NhaCungCap_CapNhat(NhaCungCap ncc)
        {
            return ws.NhaCungCap_CapNhat(ncc);
        }

        [WebMethod]
        public List<NhaCungCap> NhaCungCap_TraCuu(NhaCungCap ncc)
        {
            if(ncc.MaNhaCungCap == -1)
                return ws.NhaCungCap_TraCuu(ncc);
            else
            {
                List<NhaCungCap> listNCC = ws.NhaCungCap_TraCuu(ncc);

                if(ncc.NhaCungCap_Ten != "")
                {
                    listNCC.RemoveAll(nc => !nc.NhaCungCap_Ten.Contains(ncc.NhaCungCap_Ten));
                }
                if (ncc.NhaCungCap_DiaChi != "")
                {
                    listNCC.RemoveAll(nc => !nc.NhaCungCap_DiaChi.Contains(ncc.NhaCungCap_DiaChi));
                }
                if (ncc.NhaCungCap_Email != "")
                {
                    listNCC.RemoveAll(nc => nc.NhaCungCap_Email != ncc.NhaCungCap_Email);
                }
                if (ncc.NhaCungCap_SoDienThoai != "")
                {
                    listNCC.RemoveAll(nc => !nc.NhaCungCap_SoDienThoai.Contains(ncc.NhaCungCap_SoDienThoai));
                }
                if (ncc.NhaCungCap_Website != "")
                {
                    listNCC.RemoveAll(nc => nc.NhaCungCap_Website != ncc.NhaCungCap_Website);
                }

                return listNCC;
            }
        }

        [WebMethod]
        public bool NhanVien_Them(NhanVien nv)
        {
            return ws.NhanVien_Them(nv);
        }

        [WebMethod]
        public bool NhanVien_Xoa(NhanVien nv)
        {
            return ws.NhanVien_Xoa(nv);
        }

        [WebMethod]
        public bool NhanVien_CapNhat(NhanVien nv)
        {
            return ws.NhanVien_CapNhat(nv);
        }

        [WebMethod]
        public bool NhanVien_DoiMatKhau(NhanVien nv)
        {
            return ws.NhanVien_DoiMatKhau(nv);
        }

        [WebMethod]
        public List<NhanVien> NhanVien_TraCuu(NhanVien nv)
        {
            if(nv.MaNhanVien == -1)
                return ws.NhanVien_TraCuu(nv);
            else
            {
                List<NhanVien>listNV = ws.NhanVien_TraCuu(nv);

                if(nv.MaLoaiNhanVien != 0)
                {
                    listNV.RemoveAll(n => n.MaLoaiNhanVien != nv.MaLoaiNhanVien);
                }
                if (nv.NhanVien_DiaChi != "")
                {
                    listNV.RemoveAll(n => !n.NhanVien_DiaChi.Contains(nv.NhanVien_DiaChi));
                }
                if (nv.NhanVien_Email != "")
                {
                    listNV.RemoveAll(n => n.NhanVien_Email != nv.NhanVien_Email);
                }
                if (nv.NhanVien_Ho != "")
                {
                    listNV.RemoveAll(n => !n.NhanVien_Ho.Contains(nv.NhanVien_Ho));
                }
                if (nv.NhanVien_Luong != 0)
                {
                    listNV.RemoveAll(n => n.NhanVien_Luong != nv.NhanVien_Luong);
                }
                if (nv.NhanVien_NgaySinh != null)
                {
                    List<NhanVien> list2 = new List<NhanVien>();
                    DateTime dt = (DateTime)nv.NhanVien_NgaySinh;
                    foreach (NhanVien hd in listNV)
                    {
                        DateTime dt1 = (DateTime)hd.NhanVien_NgaySinh;
                        if (dt1.Date == dt.Date)
                        {
                            NhanVien hdx = new NhanVien();
                            hdx = hd;
                            list2.Add(hdx);
                        }
                    }
                    listNV = new List<NhanVien>();
                    listNV= list2;
                }
                if (nv.NhanVien_Phai != "")
                {
                    listNV.RemoveAll(n => n.NhanVien_Phai != nv.NhanVien_Phai);
                }
                if (nv.NhanVien_SoDienThoai != "")
                {
                    listNV.RemoveAll(n => !n.NhanVien_SoDienThoai.Contains(nv.NhanVien_SoDienThoai));
                }
                if (nv.NhanVien_Ten != "")
                {
                    listNV.RemoveAll(n => !n.NhanVien_Ten.Contains(nv.NhanVien_Ten));
                }
                if (nv.NhanVien_TenDangNhap != "")
                {
                    listNV.RemoveAll(n => n.NhanVien_TenDangNhap != nv.NhanVien_TenDangNhap);
                }
                if (nv.NhanVien_TenLot != "")
                {
                    listNV.RemoveAll(n => !n.NhanVien_TenLot.Contains(nv.NhanVien_TenLot));
                }


                return listNV;
            }
        }

        [WebMethod]
        public bool NhanVien_KiemTraTenDangNhap(NhanVien nv)
        {
            return ws.NhanVien_KiemTraTenDangNhap(nv);
        }

        [WebMethod]
        public NhanVien NhanVien_LayNV(NhanVien nv)
        {
            return ws.NhanVien_LayNV(nv);
        }

        [WebMethod]
        public NhanVien NhanVien_LayNVBangTenDangNhap(NhanVien nv)
        {
            return ws.NhanVien_LayNVBangTenDangNhap(nv);
        }

        [WebMethod]
        public LoaiNhanVien NhanVien_LayLoaiNV(NhanVien nv)
        {
            return ws.NhanVien_LayLoaiNV(nv);
        }

        [WebMethod]
        public bool NhanVien_KiemTraDangNhap(NhanVien nv)
        {
            return ws.NhanVien_KiemTraDangNhap(nv);
        }

        [WebMethod]
        public List<LoaiNhanVien> LoaiNhanVien_LayDanhSach()
        {
            return ws.LoaiNhanVien_LayDanhSach();
        }


        [WebMethod]
        public bool HoaDonBanHang_Them(HoaDonBanHang hdbh)
        {
            return ws.HoaDonBanHang_Them(hdbh);
        }

        [WebMethod]
        public HoaDonBanHang HoaDonBanHang_LayHDGanNhat()
        {
            return ws.HoaDonBanHang_LayHDGanNhat();
        }

        [WebMethod]
        public bool HoaDonBanHang_Xoa(HoaDonBanHang hdbh)
        {
            return ws.HoaDonBanHang_Xoa(hdbh);
        }

        [WebMethod]
        public List<HoaDonBanHang> HoaDonBanHang_TraCuu(HoaDonBanHang hdbh)
        {
            if (hdbh.MaHoaDon == -1)
                return ws.HoaDonBanHang_TraCuu(hdbh);
            else
            {
                List<HoaDonBanHang> listHDBH = ws.HoaDonBanHang_TraCuu(hdbh);

                if(hdbh.MaNhanVien != 0)
                {
                    listHDBH.RemoveAll(p => p.MaNhanVien != hdbh.MaNhanVien);
                }
                if (hdbh.HDBH_ThanhTien != 0)
                {
                    listHDBH.RemoveAll(p => p.HDBH_ThanhTien != hdbh.HDBH_ThanhTien);
                }
                if (hdbh.HDBH_NgayLap != null)
                {
                    List<HoaDonBanHang> list2 = new List<HoaDonBanHang>();
                    DateTime dt = (DateTime)hdbh.HDBH_NgayLap;
                    foreach (HoaDonBanHang hd in listHDBH)
                    {
                        DateTime dt1 = (DateTime)hd.HDBH_NgayLap;
                        if (dt1.Date == dt.Date)
                        {
                            HoaDonBanHang hdx = new HoaDonBanHang();
                            hdx = hd;
                            list2.Add(hdx);
                        }
                    }
                    listHDBH = new List<HoaDonBanHang>();
                    listHDBH = list2;
                }

                return listHDBH;
            }
        }

        [WebMethod]
        public bool ChiTietHoaDonBanHang_Them(ChiTietHoaDonBanHang cthdbh)
        {
            return ws.ChiTietHoaDonBanHang_Them(cthdbh);
        }

        [WebMethod]
        public bool ChiTietHoaDonBanHang_Xoa(ChiTietHoaDonBanHang cthdbh)
        {
            return ws.ChiTietHoaDonBanHang_Xoa(cthdbh);
        }

        [WebMethod]
        public bool ChiTietHoaDonBanHang_CapNhat(ChiTietHoaDonBanHang cthdbh)
        {
            return ws.ChiTietHoaDonBanHang_CapNhat(cthdbh);
        }

        [WebMethod]
        public List<ChiTietHoaDonBanHang> ChiTietHoaDonBanHang_TraCuu(HoaDonBanHang hdbh)
        {
            return ws.ChiTietHoaDonBanHang_TraCuu(hdbh);
        }






        [WebMethod]
        public bool KiemTraInt(string s)
        {
            try
            {
                int.Parse(s);
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        [WebMethod]
        public bool KiemTraDouble(string s)
        {
            try
            {
                double.Parse(s);
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
    }
}
